<link rel="stylesheet" href="__STATIC__/web/layui/css/layui.css" media="all">
<script type="text/javascript" src="__STATIC__/global/jQuery/jquery-2.2.3.min.js"></script>
<script src="__STATIC__/web/layui/layui.js"></script>

<form class="layui-form" action="{:url('user_weight_log_add')}" style="margin-top:20px;" onsubmit="return false;">
    <div class="layui-form-item">
        <label class="layui-form-label">输入框</label>
        <div class="layui-input-block">
            <input type="number" name="weight" required  lay-verify="required" placeholder="请输入重量 (斤)" autocomplete="off" class="layui-input" autofocus="autofocus" style="width:140px;">
            <input type="hidden" name="user_id" value="{$user_id}">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        </div>
    </div>
</form>

<script>
    //Demo
    var is_return = 1;
    layui.use('form', function(){
        var form = layui.form();
        //监听提交
        form.on('submit(formDemo)', function(data){
            if(!is_return){
                return false;
            }
            var layer_index = layer.load(1);
            is_return = 0;
            $.ajax({
                type:'post',
                url:"{:url('user_weight_log_add')}",
                data:data.field,
                dataType:'json',
                success:function (data) {
                    if(data.err){
                        layer.msg(data.msg, {icon:5, time:2000});
                        is_return = 1;
                    }else{
                        layer.msg(data.msg, {icon:6, time:2000});
                        setTimeout("close_layer();", 2100);
                    }
                    layer.close(layer_index);
                }
            });
            return false;
        });
    });

    function close_layer(){
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }
</script>